package redis

import (
	"context"
	"time"

	"github.com/redis/go-redis/v9"
	"github.com/samber/do/v2"
	"github.com/spf13/viper"
)

func Init(i do.Injector) (*redis.Client, error) {
	client := redis.NewClient(&redis.Options{
		Addr: viper.GetString("redis.addr"),
		DB:   viper.GetInt("redis.db"),
		//PoolSize:     viper.GetInt("redis.pool_size"),
		//MinIdleConns: viper.GetInt("redis.min_idle_conns"),
		//ReadTimeout:  viper.GetDuration("redis.read_timeout"),
		//WriteTimeout: viper.GetDuration("redis.write_timeout"),
	})

	// 测试连接
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := client.Ping(ctx).Err(); err != nil {
		return nil, err
	}
	return client, nil
}
